//
//  MWApplicationCell.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/10/25.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWApplicationCell.h"
#import "MWApplicationPictureCell.h"

@interface MWApplicationCell () <UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) UIView *containerView;
@property(nonatomic, strong) UIView *titleView;
@property(nonatomic, strong) UILabel *titleLB;
@property(nonatomic, strong) UICollectionView *collectionView;

@property(nonatomic, strong) NSMutableArray *urlArray;

@property (nonatomic, copy) NSString *navTitleString;

@end

@implementation MWApplicationCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupPageSubviews];
        [self layoutPageSubviews];
        return self;
    }
    return self;
}

- (void)setupData:(MWApplicationModel *)application {
    _titleLB.text = application.content;
    self.navTitleString = application.content;
    [self.urlArray removeAllObjects];
    [self.urlArray addObjectsFromArray:application.images];
    [_collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.urlArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWApplicationPictureCell";
    MWApplicationPictureCell *pictureCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    [pictureCell setUrlPath:self.urlArray[indexPath.row]];
    return pictureCell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 13;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(100, 100);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *datas = [NSMutableArray array];
    for (int i = 0; i < self.urlArray.count; i++) {
        YBIBImageData *browseModel = [[YBIBImageData alloc] init];
        browseModel.allowSaveToPhotoAlbum = YES;
        browseModel.imageURL =[NSURL URLWithString:[self.urlArray[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [datas addObject:browseModel];
    }
    
    YBImageBrowser *browser = [[YBImageBrowser alloc] init];
//    if ([MWConfigManager manager].language == 0) {
//        [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeSimplifiedChinese;
//    } else {
//        [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeEnglish;
//    }
    browser.defaultToolViewHandler.topView.operationType = YBIBTopViewOperationTypeSave;
    browser.dataSourceArray = datas;
    browser.currentPage = indexPath.row;
    [browser show];

}

- (NSMutableArray *)urlArray {
    if (!_urlArray) {
        _urlArray = [[NSMutableArray alloc] init];
    }
    
    return _urlArray;
}

- (void)setupPageSubviews {
    _containerView = [[UIView alloc] init];
    _containerView.layer.cornerRadius = 8;
    _containerView.layer.masksToBounds = YES;
    _containerView.layer.borderWidth = 0.5;
    _containerView.layer.borderColor = MWHexColor(@"#EEEEEE").CGColor;
    [self.contentView addSubview:_containerView];
    
    _titleView = [[UIView alloc] init];
    _titleView.backgroundColor = MWHexColor(@"#74718B");
    [self.containerView addSubview:_titleView];
    
    _titleLB = [[UILabel alloc] init];
    _titleLB.font = MWFont(15);
    _titleLB.textColor = [UIColor whiteColor];
    [self.containerView addSubview:_titleLB];
        
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[MWApplicationPictureCell class] forCellWithReuseIdentifier:@"MWApplicationPictureCell"];
    [self.contentView addSubview:_collectionView];
}

- (void)layoutPageSubviews {
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(16, 16, 0, 16));
    }];
    
    [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.containerView);
        make.height.equalTo(@40);
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleView).offset(12);
        make.centerY.equalTo(self.titleView);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLB.mas_bottom).offset(22);
        make.left.equalTo(self.containerView).offset(12);
        make.right.equalTo(self.containerView).offset(-12);
        make.bottom.equalTo(self.containerView).offset(-16);
        make.height.equalTo(@100);
    }];
}

@end
